package com.sp.system.user.zuul.interceptor;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

/**
 * @author chenanhai
 * @Describle zuul 权限拦截
 * @Date 2019年12月18日
 */
@Slf4j
public class AccessTokenFilter extends ZuulFilter {
    private AntPathMatcher matcher = new AntPathMatcher(System.getProperty("file.separator"));

    /**
     * pre：可以在请求被路由之前调用
     * routing： 路由请求时被调用
     * post：在routing和error过滤器之后被调用
     *
     * @return
     */
    @Override
    public String filterType() {

        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;// 优先级为0，数字越大，优先级越低
    }

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        return (boolean) ctx.get("isSuccess");
//        return true;// 是否执行该过滤器，此处为true，说明需要过滤
    }

    @Override
    public Object run() throws ZuulException {

        log.debug("我进入第二个filter");
        /*RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        String url = request.getRequestURI();
        String[] antMatchers = JwtTokenUtil.antMatchers.split(",");
        List<String> antMatcherList = Arrays.asList(antMatchers);
        for (String antMatch : Arrays.asList(antMatchers)) {
            if (matcher.match(antMatch, url)) {
                ctx.setSendZuulResponse(true);
                ctx.setResponseStatusCode(200);
                ctx.set("isSuccess", true);
                return null;
            }
        }


        log.debug(request.getRequestURI());*/


        return null;
    }
}
